package com.wh.wisdomsite.xbox.common.util;

import com.wh.wisdomsite.xbox.common.model.SignRule;
import com.wh.wisdomsite.xbox.utils.encrypt.hmacSHA1.HmacSHA1;
import com.wh.wisdomsite.xbox.utils.Base64;
import com.wh.wisdomsite.xbox.utils.json.JsonUtil_Google;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Map;


/**
 * 生成xbox签名
 * @Package com.wh.wisdomsite.xbox.client
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
public class XboxSign {

    private static final Log logger = LogFactory.getLog(XboxSign.class);

    /***
     * 生成签名规则
     * @param paramMap 参数集
     * @param signKey  签名密码
     * @return
     */
    public static String signRule(Map<String, String> paramMap, String signKey) {
        logger.info("生成签名-->>开始");
        //获取参数
        String client = paramMap.get("client");
        String callType = paramMap.get("callType");
        String callVersion = paramMap.get("callVersion");
        String appKey = paramMap.get("appKey");
        String format = paramMap.get("format");
        String method = paramMap.get("method");
        String version = paramMap.get("version");
        String bindObject = paramMap.get("bindObject");
        String sessionId = paramMap.get("sessionId");


        SignRule signRule = new SignRule();
        /**
         * 1: 七项基本参数 + 两项业务参数
         */
        signRule.setAppKey(appKey);
        signRule.setBindObject(bindObject.replaceAll("\r|\n|\t", ""));
        signRule.setCallType(callType);
        signRule.setCallVersion(callVersion);
        signRule.setClient(client);
        signRule.setFormat(format);
        signRule.setMethod(method);
        signRule.setSessionId(sessionId);
        signRule.setVersion(version);

        //获取签名报文
        String signString = JsonUtil_Google.getJsonStringByObject(signRule);
        logger.info("createSign-->>签名报文signString :" + signString);

        //获取签名结果
        String signStr = HmacSHA1.Encrypt(signString, signKey);
        String signValue = Base64.encodeToString(signStr.getBytes(), 2);
        logger.info("createSign-->>签名密码signValue : " + signValue);

        return signValue;
    }
}
